package com.wjm.snackbar_textInputLayout_floatingActionButton;

import android.os.Bundle;
import android.support.design.widget.Snackbar;
import android.support.design.widget.TextInputLayout;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    private LinearLayout constraintLayout;
    private TextInputLayout til, til_password;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        findViewById();
    }

    private void findViewById() {
        constraintLayout = (LinearLayout) findViewById(R.id.linearLayout);
        til = (TextInputLayout) findViewById(R.id.til);
        til_password = (TextInputLayout) findViewById(R.id.til_password);
    }

    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.btn) {
            showSnackbar();
        }
    }

    private void showSnackbar() {
        Snackbar.make(constraintLayout, "标题", Snackbar.LENGTH_LONG)
                .setAction("点击事件", new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Toast.makeText(MainActivity.this, "哈哈", Toast.LENGTH_SHORT).show();
                    }
                }).setDuration(Snackbar.LENGTH_LONG).show();
    }

    public void login(View view) {
        String userName = til.getEditText().getText().toString();
        String passWord = til_password.getEditText().getText().toString();
        if (validateUser(userName)) {
            til.setErrorEnabled(true);
            til.setError("请输入正确的用户名123");
        } else if (validatePass(passWord)) {
            til.setErrorEnabled(false);
            til_password.setErrorEnabled(true);
            til_password.setError("请输入正确的密码456");
        } else {
            til.setErrorEnabled(false);
            til_password.setErrorEnabled(false);
            Toast.makeText(this, "登录成功", Toast.LENGTH_SHORT).show();
        }
    }

    private boolean validateUser(String content) {
        if (!"123".equals(content)) {
            return true;
        }
        return false;
    }

    private boolean validatePass(String content) {
        if (!"456".equals(content)) {
            return true;
        }
        return false;
    }

    public void floatingActionButton(View view) {
        Toast.makeText(this,"我就是个Button啊，看什么看",Toast.LENGTH_SHORT).show();
    }
}
